<template>
    <ynet-content id="typhoonMain" :headerInformation="true" :headerMenu="true" class="main-container">
        <div ><!--提示当前位置-->
			<ynet-breadcrumb></ynet-breadcrumb>
		</div>
        <div >
            <el-form ref="timeForm" :model="timeForm" class="formEmbedTable mt20" :rules="rules">
                <table>
                    <caption>Enquiry</caption>
                    <tr class="mb50">
                        <td>Tropical Cyclone Warming</td>
                        <td colspan='3'>
                            <el-form-item prop="typhoonFlag">
                                <el-radio-group v-model="timeForm.typhoonFlag">
                                    <el-radio label="0">Effictive</el-radio>
                                    <el-radio label="1">Withdrawal</el-radio>
                                </el-radio-group>
                                <!--
                                <el-select v-model="timeForm.typhoonFlag" clearable>
                                    <el-option v-for="item in paramChannels" :label="item.label" :value="item.value" :key="item.value">
                                    </el-option>
                                </el-select>
                                -->
                            </el-form-item>
                        </td>					
                    </tr>
                    <tr class="mt20">
                        <td>Effictive Date </td>
                        <td>
                            <el-form-item prop="startDateObj">
                                <el-date-picker v-model="timeForm.startDateObj" type="datetime" :picker-options="pickerOptions" @change="selectStartTime" format="yyyy-MM-dd HH:mm"></el-date-picker>
                            </el-form-item>
                        </td>		
                        <td>Withdrawal Date</td>
                        <td >
                            <el-form-item prop="endDateObj">
                                <el-date-picker v-model="timeForm.endDateObj" type="datetime" :picker-options="pickerOptions" @change="selectEndTime" format="yyyy-MM-dd HH:mm"></el-date-picker>
                            </el-form-item>
                        </td>					
                    </tr>
                </table>
            </el-form>
        </div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" @click="queryAuth('timeForm')">Confirm</el-button>
        </div>
        <div  v-if="main">
            
        </div>
        <!--修改区域-->        
        <div v-if="isEdit">
            <AccessEdit @back="back" :editData="editData"></AccessEdit>
        </div>
        <!--授权确认页面弹窗-->
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
        
        
    </ynet-content>
</template>

<script>
    import TyphoonAdd from './components/typhoonAdd';
    //import AccessAdd from './components/accessAdd';
	export default {
		name:'typhoonMain',
		components:{
          TyphoonAdd,
		},
		data(){
            return { 
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < Date.now() - 8.64e7;
                    }
                },
                main:true,turnPageTotalNum:'',dialogTableVisible:false,
                isAdd:false,
                isEdit:false,
                accessList:[],//从后台查询到的白名单集合
                rules:{
                    typhoonFlag:[
                        {required:true,message:'required',trigger:'blur'},
                    ],
                    startDateObj:[
                        //{required:true,message:'不能为空',trigger:'blur'},
                        {trigger: 'blur', validator:this.$tools.validator.timeCompare,compareList:['startDateObj','endDateObj'],vm:this,formName:'timeForm'}
                    ],
                    endDateObj:[
                        //{required:true,message:'不能为空',trigger:'blur'},
                        {trigger: 'blur', validator:this.$tools.validator.timeCompare,compareList:['startDateObj','endDateObj'],vm:this,formName:'timeForm'}
                    ],
                },
                timeForm:{
                    typhoonFlag:'',
                    startDateObj:'',
                    startTime:'',
                    endDateObj:'',
                    endTime:'',
                },
                 paramChannels:[
                    {label:'Effictive',value:'0'},
                    {label:'Withdrawal',value:'1'},
                ],
                needAuth:'',
                authType:'', 
                editData:{},
                delForm:{
                    bsnCode:'IM0205',
                },
                authForm: { authUserNo: '', password: '',randomField:''},//100004   617100
                queryForm:{
                    paramChannel:'',paramCode:'',bsnCode:'IM0205',currentPage:'1',turnPageShowNum:'10'
                },
                typhoonList:[],
                addForm:{
                    typhoonFlag:'',startTime:'',endTime:'',bsnCode:'IM0205',actionType:'0',paramCode:'',
                },
            }   
        },
        mounted(){
            this.queryChange();
        },
		methods:{ 
            selectStartTime(s){//选择的开始时间
                console.log("s"+s)
                this.timeForm.startDateObj=s
            },
            selectEndTime(s){//结束时间
                this.timeForm.endDateObj=s
            },
            getAuthForm(val){
                //获取授权数据
                this.authForm=val;
                console.log("val:"+JSON.stringify(val))
                this.addForm=$.extend(this.addForm,this.authForm)
                this.typhoonAdd()  //自己页面提交给后台的.do方法
            },
            queryAuth(formName){//查询授权模式
                console.log(111)
                this.addForm.typhoonFlag=this.timeForm.typhoonFlag;
                console.log(222)
                this.timeForm.startTime=this.$tools.utils.formatDate(this.timeForm.startDateObj,{toMinite:true})
                console.log(333)
                this.timeForm.endTime=this.$tools.utils.formatDate(this.timeForm.endDateObj,{toMinite:true})
                console.log(444)
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        let body={};body.bsnCode="IM0205";body.version="IM0205",
                        this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                            if(data.body.errorCode==0){
                                if(data.body.needAuth==0){//无需授权
                                    this.typhoonAdd()
                                }else if(data.body.needAuth==1 && data.body.authType==1){
                                    this.needAuth=1;
                                    this.authType=1;
                                    this.$refs["dialogTable"].show()
                                            // this.dialogTableVisible=true;
                                }else if( data.body.needAuth==1 && data.body.authType==2 ){
                                    this.needAuth=1;
                                    this.authType=2;
                                    this.typhoonAdd()
                                }else{
                                    this.$alert('Authorization Type Error', {
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    });
                                }
                            }else{
                                this.$alert("System Error Try again", "warning", {
                                    confirmButtonText: 'Confirm',
                                    callback: action => {
                                    }
                                });
                            }
                        })   
                    }
                })
                
			},
            typhoonAdd(){//点击新增按钮,显示新增页面
                this.addForm.startTime= this.timeForm.startTime.substring(0, 4) + "" + this.timeForm.startTime.substring(5, 7) + ""+ this.timeForm.startTime.substring(8, 10)+ ""+ this.timeForm.startTime.substring(11, 13)+ ""+ this.timeForm.startTime.substring(14, 16)  
                this.addForm.endTime=this.timeForm.endTime.substring(0, 4) + "" + this.timeForm.endTime.substring(5, 7) + ""+ this.timeForm.endTime.substring(8, 10) + ""+ this.timeForm.endTime.substring(11, 13) + ""+ this.timeForm.endTime.substring(14, 16)  
                console.log("addForm:"+JSON.stringify(this.addForm))
                this.$tools.request(this,"IM0205Action.do",this.addForm).then(data=>{
                    if(data.body.errorCode==0){
                        if(this.needAuth==1 && this.authType==2){
                            this.$message({
								type:'success',
								message:'Waiting for Authorized'
							})
                            this.$emit("back")
                        }else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message({
                                    type:'warning',
                                    message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                                this.$emit("back")
                            }else{
                                this.$message({
                                    type:'success',
                                    message:'Delete Success',
                                    showClose:true,
                                })
                                this.$emit("back")
                            }
                        }else{
                            this.$message({
                                type:'success',
                                message:'Success',
                                showClose:true,
                            })
                        }
                        
                    }else{
                        this.$alert("System Error Try again", "warning",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })
            },
            back(){
                this.queryChange();
            },
            queryChange(){
                this.$tools.request(this,"IM0205QueryAction.do",this.queryForm).then(data=>{
                    console.log(JSON.stringify(data))
                    if(data.body.errorCode==0 && data.body.typhoonList.length!=0){
                        this.typhoonList=data.body.typhoonList
                        //this.addForm.paramCode=this.typhoonList[0].paramCode
                        this.timeForm.typhoonFlag=this.typhoonList[0].typhoonFlag
                        this.timeForm.startDateObj=this.$tools.utils.formatDateSymbol(this.typhoonList[0].startTime,"YYYY-MM-DD HH:mm")
                        this.timeForm.endDateObj=this.$tools.utils.formatDateSymbol(this.typhoonList[0].endTime,"YYYY-MM-DD HH:mm")
                    }
                    
                })
            },
            
		}
	}
</script>